package com.gm.wholesale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import androidx.annotation.Nullable;
import androidx.core.splashscreen.SplashScreen;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.module.config.ConfigData;
import com.gm.wholesale.module.config.bean.ConfigBean;
import com.gm.wholesale.module.config.model.ConfigModel;
import com.gm.wholesale.module.login.ui.LoginActivity;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.user.UserDataManger;

public class SplashActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        SplashScreen.installSplashScreen(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        String token = UserDataManger.newInstance().getToken();
        if (!TextUtils.isEmpty(token)) {
            getConfig();
        } else {
            startLoginAct();
        }
    }


    private void getConfig() {

        ConfigModel.getConfig(null, new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(DataResp<ConfigBean> resp) {
                ConfigData.getConfigData().setConfigBean(resp.getData());
                startMainActivity();
                finish();
            }

            @Override
            protected void onError(String error) {
                ToastUtil.showToast(error);
                startLoginAct();
                finish();
            }

        });
    }


    //todo 跳转到主界面
    private void startMainActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    private void startLoginAct() {
        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            System.exit(0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
